<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);

class siteSetting extends admin {
	private $db;
	function __construct() {
		parent::__construct();
		pc_base::load_app_func('global');
		$this->db = pc_base::load_model('site_setting_model');
		$this->siteid = get_siteid();
	}
	
	/**
	 *变量列表
	 */
	public function init() {
		$where = 'site='.$this->siteid;
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$label = $this->db->listinfo($where, 'id DESC', $page, 15);
		$pages = $this->db->pages;
		$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=admin&c=siteSetting&a=add\', title:\''.'添加变量'.'\', width:\'580\', height:\'420\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', "添加变量");
		include $this->admin_tpl('setting_list');
	}
	
	/**
	 * 添加变量
	 */
	public function add() {
		pc_base::load_sys_class('form', '', 0);
		$type=['1'=>'单个值',"2"=>"多个值(json)","3"=>"多值(|分割)"];
		if (isset($_POST['dosubmit']) && !empty($_POST['dosubmit'])) {
			$data['s_key'] = $_POST['info']['s_key'];
			$data['s_value'] = $_POST['info']['s_value'];
			$data['description'] = $_POST['info']['description'];
			$data['type'] = $_POST['info']['type'];
			$data['sort'] = $_POST['info']['sort'];
			$data['site'] = $this->siteid;
			if ($this->db->insert($data)) {
			    $id = $this->db->insert_id();
			    //记录行为日志
			    $log_content = '<span style="color:green;">添加网站变量</span>[变量id：'.$id.'；变量名称：'.$_POST['info']['s_key'].']';
			    $this->manage_admin_log($log_content);
				showmessage(L('success'),'','','add');
			}else {
				showmessage(L('error'));
			}
		}else {
			$show_header = $show_validator = '';
			include $this->admin_tpl('setting_add');
		}
	}
	
	/**
	 * 编辑变量
	 */
	public function edit() {
		pc_base::load_sys_class('form', '', 0);
		$type=['1'=>'单个值',"2"=>"多个值(json)","3"=>"多值(|分割)"];
		$id = isset($_GET['id']) ? $_GET['id'] : showmessage("id不存在", HTTP_REFERER);
		$setting = $this->db->get_one(array('id'=>$id));
		if (isset($_POST['dosubmit']) && !empty($_POST['dosubmit'])) {
			$data['s_key'] = $_POST['info']['s_key'];
			$data['s_value'] = $_POST['info']['s_value'];
			$data['description'] = $_POST['info']['description'];
			$data['type'] = $_POST['info']['type'];
			$data['sort'] = $_POST['info']['sort'];
			$data['site'] = $this->siteid;
			if ($this->db->update($data,array('id'=>$id))) {
			    //记录行为日志
			    $log_content = '<span style="color:blue;">修改网站变量</span>[变量id：'.$id.'；变量名称：'.$_POST['info']['s_key'].']';
			    $this->manage_admin_log($log_content);
				showmessage(L('update_success'),'','','edit');
			}else {
				showmessage(L('update_error'));
			}
		}else {
			$show_header = $show_validator = '';
			include $this->admin_tpl('setting_edit');
		}
	}
	
	/**
	 * 删除变量
	 */
	public function del() {
		$id = $_GET['id'];
		$s_key = $_GET['s_key'];
		if($this->db->delete(array('id'=>$id))) {
		    //记录行为日志
		    $log_content = '<span style="color:red;">删除网站变量</span>[变量id：'.$id.'；变量名称：'.$s_key.']';
		    $this->manage_admin_log($log_content);
			exit('1');
		} else {
			exit('0');
		}
	}
	
	/**
	 * 批量删除变量
	 */
	public function del_all() {
		$where = "";
		$id = $_POST['id'];
		$ids = '';
		foreach ($id as $k=>$v){
			if (intval($v)) {
				$id[$k] = intval($v);
				$ids .= ','.$v;
			} else {
				unset($id[$k]);
			}
		}
		$id = implode('\',\'', $id);
		$ids = substr($ids, 1,(strlen($ids)-1));
		$where .= "id in ('$id')";
		$s_key = $this->db->select($where,'s_key');
		$s_key_str = '';
		foreach($s_key as $key=>$value){
		    $s_key_str .= $value["s_key"].',';
		}
		$s_key_str = substr($s_key_str, 0,(strlen($s_key_str)-1));
		if ($this->db->delete($where)) {
		    //记录行为日志
		    $log_content = '<span style="color:red;">删除多个网站变量</span>[变量id：'.$ids.'；变量名称：'.$s_key_str.']';
		    $this->manage_admin_log($log_content);
			showmessage(L('siteSetting_del_success'),HTTP_REFERER);
		}else {
			showmessage(L('siteSetting_del_error'),HTTP_REFERER);
		}
	}
}